package com.wsoft.bpm.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.log4j.Log4j2;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工具类
 *
 * @Author zhonglj
 * @Date 2023/11/22 9:32
 */
@Log4j2
public class CommonUtil {
    /**
     * 生成随机7位密码
     *
     * @param
     * @return java.lang.String
     * @author zhonglj
     * @date 2023-12-15 17:20
     **/
    public static String getRandomPwd() {
        String c = RandomUtil.randomString("qwertyuioplkjhgfdsazxcvbnm", 2);
        String f = RandomUtil.randomString("!#$%&*=+", 2);
        String s = RandomUtil.randomString("1234567890", 3);

        return c + f + s;
    }

    /**
     * 版本号 数字转中文
     *
     * @param numberList
     * @return
     */
    public static List<Map<String, String>> ConvertNumberToChinese(List<String> numberList) {

        //数字对应的汉字
        String[] num = {"一", "二", "三", "四", "五", "六", "七", "八", "九"};
        //单位
        String[] unit = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万亿"};
        List<Map<String, String>> result = new ArrayList<>();

        //将输入数字转换为字符串
        //将该字符串分割为数组存放
        for (String number : numberList) {
            Map<String, String> resmap = new HashMap<>();
            char[] ch = number.toCharArray();
            //结果 字符串
            String str = "";
            int length = ch.length;
            for (int i = 0; i < length; i++) {
                int c = (int) ch[i] - 48;
                if (c != 0) {
                    str += num[c - 1] + unit[length - i - 1];
                }
            }
            resmap.put("value", number);
            resmap.put("name", "第" + str + "版");
            result.add(resmap);
        }
        return result;
    }

    /**
     * 为空则返回0，否则返回自身
     *
     * @param decimal
     * @return java.math.BigDecimal
     * @author zhonglj
     * @date 2024-06-05 13:22
     **/
    public static BigDecimal optZero(BigDecimal decimal) {
        return Opt.ofNullable(decimal).orElse(BigDecimal.ZERO);
    }

    /**
     * 为空则返回0，否则返回自身
     *
     * @param decimal
     * @return java.math.BigDecimal
     * @author zhonglj
     * @date 2024-06-05 13:22
     **/
    public static BigDecimal optZero(String decimal) {
        if (StrUtil.isBlank(decimal)) return BigDecimal.ZERO;
        return Opt.ofNullable(new BigDecimal(decimal)).orElse(BigDecimal.ZERO);
    }

    private static BigDecimal div(BigDecimal num1, BigDecimal num2, int scale, RoundingMode roundingMode) {
        if (num2.equals(BigDecimal.ZERO)) return BigDecimal.ZERO;
        return NumberUtil.div(num1, num2, scale, roundingMode);
    }

    public static BigDecimal divDefault(Object num1, Object num2) {
        BigDecimal decimal1 = null, decimal2 = null;
        if (!(num1 instanceof BigDecimal)) {
            decimal1 = Convert.convert(BigDecimal.class, num1);
        } else {
            decimal1 = (BigDecimal) num1;
        }

        if (!(num2 instanceof BigDecimal)) {
            decimal2 = Convert.convert(BigDecimal.class, num2);
        } else {
            decimal2 = (BigDecimal) num2;
        }
        return div(decimal1, decimal2, 4, RoundingMode.HALF_UP);
    }

    public static String formatDuration(Long milliseconds) {
        long days = milliseconds / DateUnit.DAY.getMillis();
        milliseconds %= DateUnit.DAY.getMillis();

        long hours = milliseconds / DateUnit.HOUR.getMillis();
        milliseconds %= DateUnit.HOUR.getMillis();

        long minutes = milliseconds / DateUnit.MINUTE.getMillis();
        milliseconds %= DateUnit.MINUTE.getMillis();

        long seconds = milliseconds / DateUnit.SECOND.getMillis();

        StringBuilder sb = new StringBuilder();
        if (days > 0) sb.append(days).append("天");
        if (hours > 0) sb.append(hours).append("小时");
        if (minutes > 0) sb.append(minutes).append("分");
        if (seconds > 0) sb.append(seconds).append("秒");

        return sb.length() == 0 ? "0秒" : sb.toString();
    }
}
